package com.luoxin.servicemap.remote;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.luoxin.internalcommon.constant.AmapConfigConstants;
import com.luoxin.internalcommon.dto.ResponseResult;
import com.luoxin.internalcommon.response.TerminalResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TerminalClient {
    @Value("${amap.key}")
    private String mapKey;
    @Value("${amap.sid}")
    private String sid;

    @Autowired
    private RestTemplate restTemplate;

    public ResponseResult<TerminalResponse> add(String name,String desc) {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TERMINAL_ADD);
        url.append("?");
        url.append("key="+mapKey);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("name="+name);
        url.append("&");
        url.append("desc="+desc);

        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        String body = forEntity.getBody();
        log.info("高德地图创建终端结果:{}", body);
        JSONObject result = JSONObject.parseObject(body);
        String errcode = result.getString("errcode");
        if (!"10000".equals(errcode)){
            String errmsg = result.getString("errmsg");
            return ResponseResult.fail(Integer.valueOf(errcode), errmsg);
        }
        JSONObject data = result.getJSONObject("data");
        String tid = data.getString("tid");
        TerminalResponse terminalResponse = new TerminalResponse();
        terminalResponse.setTid(tid);
        return ResponseResult.success(terminalResponse);
    }

    public ResponseResult deleteTerminal(String tid) {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TERMINAL_DELETE);
        url.append("?");
        url.append("key="+mapKey);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("tid="+tid);

        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        String body = forEntity.getBody();
        log.info("高德地图删除终端结果:{}", body);
        JSONObject result = JSONObject.parseObject(body);
        String errcode = result.getString("errcode");
        if (!"10000".equals(errcode)){
            String errmsg = result.getString("errmsg");
            return ResponseResult.fail(Integer.valueOf(errcode), errmsg);
        }
        return ResponseResult.success();
    }

    public ResponseResult search() {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TERMINAL_LIST);
        url.append("?");
        url.append("key="+mapKey);
        url.append("&");
        url.append("sid="+sid);

        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        String body = forEntity.getBody();
        log.info("高德地图查询终端结果:{}", body);
        JSONObject result = JSONObject.parseObject(body);
        String errcode = result.getString("errcode");
        if (!"10000".equals(errcode)){
            String errmsg = result.getString("errmsg");
            return ResponseResult.fail(Integer.valueOf(errcode), errmsg);
        }
        return ResponseResult.success();
    }

    public ResponseResult<List<TerminalResponse>> aroundSearch(String center, Integer radius) {
        StringBuilder url = new StringBuilder();
        url.append(AmapConfigConstants.TERMINAL_AROUNDSEARCH);
        url.append("?");
        url.append("key="+mapKey);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("center="+center);
        url.append("&");
        url.append("radius="+radius);

        ResponseEntity<String> forEntity = restTemplate.postForEntity(url.toString(), null, String.class);
        String body = forEntity.getBody();
        log.info("高德地图周边搜索终端结果:{}", body);
        JSONObject result = JSONObject.parseObject(body);
        String errcode = result.getString("errcode");
        if (!"10000".equals(errcode)){
            String errmsg = result.getString("errmsg");
            return ResponseResult.fail(Integer.valueOf(errcode), errmsg);
        }
        JSONObject data = result.getJSONObject("data");
        JSONArray results = data.getJSONArray("results");
        List<TerminalResponse> terminalResponseList = new ArrayList<>(results.size());
        for (int i = 0; i < results.size(); i++){
            JSONObject jsonObject = results.getJSONObject(i);
            TerminalResponse terminalResponse = new TerminalResponse();
            terminalResponse.setTid(jsonObject.getString("tid"));
            terminalResponse.setCarId(Long.parseLong(jsonObject.getString("desc")));

            JSONObject locationJsonObject = jsonObject.getJSONObject("location");
            terminalResponse.setLongitude(String.valueOf(locationJsonObject.getDouble("longitude")));
            terminalResponse.setLatitude(String.valueOf(locationJsonObject.getDouble("latitude")));

            terminalResponseList.add(terminalResponse);
        }
        return ResponseResult.success(terminalResponseList);
    }
}
